# -*-coding:utf-8-*-
# 作者    ：SunDuWei
# 创作时间 ：2020-05-18
# import pandas as pd
# from tqdm import tqdm
# df_data = pd.read_excel('数据.xlsx', sheet_name=[0,1])
#
# df_data_1 = df_data[0]
# df_data_1.rename(columns={'Unnamed: 0': 'start'}, inplace =True)
# end_columns_1 = df_data_1.columns.values.tolist()
# del end_columns_1[0]
#
# df_data_1_list = []
# for i, k in zip(range(len(df_data_1)), tqdm(range(len(df_data_1)))):
#     start = df_data_1.loc[i, 'start']
#     df_temp = pd.DataFrame(columns={'start','end','value'})
#     df_temp['end'] = end_columns_1
#     df_temp['start'] = start
#     df_temp['value'] = df_data_1.loc[i, end_columns_1].to_list()
#     df_data_1_list.append(df_temp)
# df_data_1_1 = pd.concat(df_data_1_list)
# df_data_1_1.reset_index(drop=True,inplace =True)
# df_data_1_1 = df_data_1_1[df_data_1_1['value'] != 0]# 取0代码
# df_data_1_1.reset_index(drop=True,inplace =True)
# df_data_1_1.to_excel('数据数值表.xlsx', index=True)
#
#
# df_data_2 = df_data[1]
# df_data_2.rename(columns={'Unnamed: 0': 'start'}, inplace =True)
# end_columns_2 = df_data_2.columns.values.tolist()
# del end_columns_2[0]
#
#
# df_data_2_list = []
# for i, k in zip(range(len(df_data_2)), tqdm(range(len(df_data_2)))):
#     start = df_data_2.loc[i, 'start']
#     df_temp = pd.DataFrame(columns={'start','end','value'})
#     df_temp['end'] = end_columns_2
#     df_temp['start'] = start
#     df_temp['value'] = df_data_2.loc[i, end_columns_2].to_list()
#     df_data_2_list.append(df_temp)
# df_data_1_2 = pd.concat(df_data_2_list)
# df_data_1_2 = df_data_1_1[df_data_1_1['value'] != 0]# 去0代码
# df_data_1_2.reset_index(drop=True, inplace =True)
# df_data_1_2.to_excel('数据01表.xlsx',index=True)

# %% 点找寻
import pandas as pd
from tqdm import tqdm

df_data = pd.read_excel('数据.xlsx', sheet_name=1)
df_data.rename(columns={'Unnamed: 0': 'start'}, inplace =True)
df_data = df_data.fillna(0)
# 起始点
start_list = [x for x in df_data.columns[df_data.columns.str.startswith('p_')]]
start_list_2 = [x for x in df_data.columns[df_data.columns.str.startswith('P_')]]
start_list.extend(start_list_2)
# 终止点
end_list = [x for x in df_data.columns[df_data.columns.str.startswith('d_')]]
end_list_2 = [x for x in df_data.columns[df_data.columns.str.startswith('D_')]]
end_list.extend(end_list_2)
# 中间点
all_list = df_data.columns.values.tolist()
del all_list[0]
between_list_temp = list(set(all_list).difference(end_list))
between_list = list(set(between_list_temp).difference(start_list))


def find_get_location(start):
    df_temp = df_data[df_data['start'] == start]
    if len(df_temp) == 0:
        value = []
        return value
    else:
        values = df_temp.iloc[0, :]
        del values['start']
        values = list(values[values == 1].index)
        return values


def find_connection(getable_list, begin_list, end):
    # if len(getable_list) == 0 and begin_list[0] in start_list:
    #     getable_list = find_get_location(begin_list[0])
    if len(getable_list) == 0:
        return 0
    getable_list = list(set(getable_list))
    between_list_temp = list(set(getable_list).intersection(set(between_list)))
    between_list_temp = list(set(between_list_temp).difference(set(begin_list)))
    end_list_temp = list(set(getable_list).difference(set(between_list_temp)))
    end_list_temp = list(set(end_list_temp).difference(set(begin_list)))
    if end in end_list_temp:
        return 1
    elif len(between_list_temp) != 0:
        getable_list = []
        for between_place in between_list_temp:
            temp_list = find_get_location(between_place)
            getable_list.extend(temp_list)
        begin_list.extend(between_list_temp)
        return find_connection(getable_list, begin_list, end)
    else:
        return 0


df_start_end = pd.DataFrame(columns={'start', 'end'})

with tqdm(range(len(start_list))) as t:
    for start, j in zip(start_list, t):
        for end in end_list:
            begin_list = [start]
            getable_list = find_get_location(start)
            while True:
                value = find_connection(getable_list, begin_list, end)
                if value == 0:
                    print('%s不能到达%s' % (start, end))
                    break
                if value == 1:
                    df_start_end.loc[len(df_start_end)] = [start, end]
                    print('%s可以到达%s' % (start, end))
                    break


df_start_end.to_excel('地点链接判断表.xlsx', index=False)

